<?php
namespace app\controller;
use think\Request;
use think\View;
use app\model\Alg as AlgModel;
use app\model\Adv as AdvModel;
use app\model\Info as InfoModel;
use app\common\BigWhite;
use think\facade\Filesystem;
use app\model\User as UserModel;
use app\model\Qiandao as QiandaoModel;
use app\model\Jingcai as JingcaiModel;
use app\model\Jf as JfModel;
class Index
{
    // 预测页面
     private $toast = 'public/toast';
    public function index()
    {
        echo "hello world";
    }
    public function getuserinfo(Request $request)
    {
        $wxid = $request->param('wxid');
        $user = UserModel::where('wxid', $wxid)->find();
        if($user == null ){
            $data = ['wxid' => $wxid,'nickName' => '微信用户','imguri' => '','jifen' => '0','shuinum' => 0];
            UserModel::create($data);
            $user = UserModel::where('wxid', $wxid)->find();
            return json($user);
        }else{
            $user = UserModel::where('wxid', $wxid)->find();
            return json($user);
        }
        
    }

    public function uuserimgurl(Request $request)
    {
        $imgurl = $request->param('imgurl');
        $wxid = $request->param('wxid');
        $user = UserModel::where('wxid', $wxid)->find();
        $user->imguri = $imgurl;
        if($user->save()){
            $code = 200;
            $res  = ['code' => $code,'msg' => '修改成功','data' => $user];
        }else{
            $code = 201;
            $res  = ['code' => $code,'msg' => '修改失败','data' => $user];
        }
        return json($res);
       
    }

    public function uusernickname(Request $request)
    {
        $nickName = $request->param('nickName');
        $wxid = $request->param('wxid');
        $user = UserModel::where('wxid', $wxid)->find();
        $user->nickName = $nickName;
        if($user->save()){
            $code = 200;
            $res  = ['code' => $code,'msg' => '修改成功','data' => $user];
        }else{
            $code = 201;
            $res  = ['code' => $code,'msg' => '修改失败','data' => $user];
        }
        return json($res);
    }
    public function qiandao(Request $request)
    {
        $datetime = date('Y-m-d');
        $wxid = $request->param('wxid');
        $user = QiandaoModel::where([['wxid','=',$wxid],['qiandaodata','=',$datetime]])->find();
        if($user == null ){
            $data = ['wxid' => $wxid,'qiandaodata' => $datetime];
            QiandaoModel::create($data);
            $code = 200;
            $userqiandao = JfModel::find(1);
            $userjf = UserModel::where('wxid', $wxid)->find();
            $userjf->jifen = $userjf->jifen+$userqiandao->qiandaojf;
            $userjf->shuinum = $userjf->shuinum+$userqiandao->qdshuinum;
            $userjf->save();
            $res  = ['code' => $code,'msg' => '签到成功','data'=>$userjf];
            return json($res);
        }else{
            $code = 201;
            $res  = ['code' => $code,'msg' => '今日已签到'];
            return json($res);
        }
       
    }

    public function jingcais(Request $request)
    {
        $datetime = date('Y-m-d');
        $now = date('H:i'); 
        $targetTime = '19:30';
        if (strtotime($now) > strtotime($targetTime)) {
            $code = 201;
            $res  = ['code' => $code,'msg' => '竞猜已截止'];
            return json($res);
        } else {
            $wxid = $request->param('wxid');
            $jingcai = $request->param('jingcai');
            $user = JingcaiModel::where([['wxid','=',$wxid],['jdate','=',$datetime]])->find();
            if($user == null ){
                $data = ['wxid' => $wxid,'jdate' => $datetime,'staus' => 1,'tm' => $jingcai];
                JingcaiModel::create($data);
                $code = 200;
                $res  = ['code' => $code,'msg' => '竞猜成功'];
                return json($res);
            }else{
                $code = 201;
                $res  = ['code' => $code,'msg' => '今日已竞猜'];
                return json($res);
            }
        }

    }
    public function guoshu(){
        $data = AdvModel::select();
        $res  = ['code' => 200,'msg' => '获取成功','data' => $data];
        return json($res);
    }

    public function guoshumy(Request $request){
        $wxid = $request->param('wxid');
        $user = AlgModel::where([['wxid','=',$wxid],['staus','=',1]])->find();
        $code = 200;
        if($user!=null){
             $guoshu = AdvModel::where([['id','=',$user->guoshuid]])->find();
             $res  = ['code' => $code,'data' => $user,'guoshu'=>$guoshu];
             return json($res);
        }else{
            $res  = ['code' => $code,'data' => $user,'guoshu'=>null];
            return json($res);
        }
    }
    public function guoshus(Request $request){
        $wxid = $request->param('wxid');
        $guoshuid = $request->param('guoshuid');
        $user = AlgModel::where([['wxid','=',$wxid],['staus','=',1]])->find();
        if($user == null ){
            $data = ['wxid' => $wxid,'staus' => 1,'guoshuid'=> $guoshuid,'stages'=>0];
            AlgModel::create($data);
            $code = 200;
            $gsdata = AdvModel::where([['id','=',$guoshuid]])->find();
            $res  = ['code' => $code,'msg' => '创建成功','gsdata'=>$gsdata];
            return json($res);
        }else{
            $code = 201;
            $res  = ['code' => $code,'msg' => '创建失败,当前果树未成熟'];
            return json($res);
        }
    }

    public function jiaoshui(Request $request){
        $wxid = $request->param('wxid');
        $shuinum = $request->param('shuinum');
        $user = UserModel::where('wxid', $wxid)->find();
        $user->shuinum = $user->shuinum- $shuinum;
        $user->save();
        $guoshu = AlgModel::where([['wxid','=',$wxid],['staus','=',1]])->find();
        $guoshu->stages = $guoshu->stages + $shuinum;
        $guoshu->save();
        $code = 200;
        $res  = ['code' => $code,'msg' => '浇水成功'];
        return json($res);
    }
    public function shouqu(Request $request){
        $wxid = $request->param('wxid');
        $jf = $request->param('jf');
        $issucces =$this->seaeuserjf($wxid, $jf);
        if($issucces ){
            $guoshu = AlgModel::where([['wxid','=',$wxid],['staus','=',1]])->find();
            $guoshu->staus = 3;
            $guoshu->save();
            $code = 200;
            $res  = ['code' => $code,'msg' => '成功'];
            return json($res);

        }else{
            $code = 201;
            $res  = ['code' => $code,'msg' => '失败'];
            return json($res);
        }
    }
    public function seaeuserjf($wxid,$jf){
        $user = UserModel::where('wxid', $wxid)->find();
        $user->jifen = $user->jifen + $jf;
        return $user->save();
    }
    public function seaeusershuinum($wxid,$shuinum){
        $user = UserModel::where('wxid', $wxid)->find();
        $user->shuinum = $user->shuinum + $shuinum;
        return $user->save();
    }
    public function jingcaijieguo(Request $request){
        $kjtm =intval($request->param('kjtm'));
        if($kjtm>=0 && $kjtm<=27){
            $jifen = JfModel::find(1);
            $list = JingcaiModel::where('staus', 1)->select();
            foreach($list as $key=>$user){
                if($user->tm == $kjtm){
                    // 中奖
                    $user->staus = 3;
                    $user->save();
                    //添加积分
                    //获取竞猜积分
                    $this->seaeuserjf($user->wxid,$jifen->jingcaijf);
                    $this->seaeusershuinum($user->wxid,$jifen->jcshuinum);
                }else{
                    // 没有中奖
                    $user->staus = 2;
                    $user->save();
                }
            }
            return view($this->toast, [
                'infos'     =>  ['恭喜，开奖成功~'],
                'url_text'  =>  '返回',
                'url_path'  =>  url('/jingcai')
            ]);
        }else{
            return view($this->toast, [
                'infos'     =>  ['开奖失败,请重新提交~'],
                'url_text'  =>  '返回',
                'url_path'  =>  url('/jingcai')
            ]);
        }

    }
    // 上传图片
    public function upload(){
        $file = request()->file('image');
        // 移动到框架应用根目录/uploads/ 目录下
        try{
            // 上传图片到本地服务器
            $saveName = \think\facade\Filesystem::disk('public')->putFile('img',$file);
            $saveName = str_replace('\\', '/', $saveName);
            $arr = ['code' => 200, 'msg' => '图片上传成功！', 'path' =>  $saveName];
            return json($arr);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return $this->exceptionHandle($e,'图片上传失败!' . $e->getMessage(),'json','');
        }

    }

}
